<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('新增')" />
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-edit" th:object="${task}" >
			<input name="id"  type="hidden"  th:field="*{id}" />
			<div class="form-group">
				<label class="col-sm-3 control-label"><span style="color: red; ">*</span>任务名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="name" id="name"  th:field="*{name}" required>
				</div>
			</div>
			
			<div class="form-group">
				<label class="col-sm-3 control-label">任务描述：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="remark" id="remark"  th:field="*{remark}" >
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-3 control-label">所属项目：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" readonly="readonly"  th:field="*{project.name}" >
				</div>
			</div>
			
			<div class="form-group">
				<label class="col-sm-3 control-label">所属步骤：</label>
				<div class="col-sm-8">
					<select name="step.id" id="stepId" class="form-control" th:field="*{step.id}" >
						<option th:each="step : ${tempstepList}" th:text="${step.name}" th:value="${step.id}" ></option>		
                    </select>
				</div>
			</div>
			
			
			<div class="form-group">
				<label class="col-sm-3 control-label">标签：</label>
				<div class="col-sm-8">
					<select name="label.id"  class="form-control" th:field="*{label.id}" >
                        <option th:each="label : ${labelList}" th:text="${label.name}" th:value="${label.id}" required></option>
                    </select>
				</div>
			</div>
			
			
			<div class="form-group">
				<label class="col-sm-3 control-label">优先级：</label>
				<div class="col-sm-8">
					<select name="yxj" class="form-control m-b" th:with="type=${@dict.getType('task_yxj')}" th:field="*{yxj}" >
	                	<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
	            	</select>
				</div>
			</div>
			
			
			<div class="form-group">
				<label class="col-sm-3 control-label">预计时间：</label>
				<div class="col-sm-8">
					<input type="text" data-type="datetime" th:field="*{endTime}" data-format="yyyy-MM-dd HH:mm:ss"  class="time-input form-control" id="endTime" name="endTime" required>
				</div>
			</div> 
			
			
			<div class="form-group">
				<label class="col-sm-3 control-label">附件列表：</label>
				<div class="col-sm-8">
					<div  th:each="file: ${fileList}" th:id="|div${file.id}|" >
						<span th:text="${file.name}" style="width:200px;display:inline-block"></span>
						<a th:onclick="downloadFile([[${file.path}]])" style="margin-left:20px;margin-right:20px" >下载</a>
						<a th:onclick="deleteFile([[${file.id}]])">删除</a>
					</div>
				</div>
			</div> 
			
			
			<input name="fileList" id="fileList" type="hidden" />
			
			<div class="form-group">
				<label class="col-sm-3 control-label">上传附件：</label>
				<input type="file" name="files" multiple onchange="uploadFile()" >
			</div>
			
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<script type="text/javascript">
		var prefix = ctx + "task/task";
		
		function submitHandler() {
	        if ($.validate.form()) {
	        	$.operate.save(prefix + "/edit", $('#form-edit').serialize());
	        }
	    }
		
		$("#form-edit").validate({
			onkeyup: false,
			rules:{
				sort:{
					digits:true
				}
			},
			messages: {
		        
		    },
		    focusCleanup: true
		});
		
		var fileList = []
		function uploadFile(){
			var formdata = new FormData(document.getElementById("form-edit"));
		    $.ajax({
		        url: prefix + "/uploadFile",
		        data: formdata,
		        type: "post",
		        processData: false,
		        contentType: false,
		        success: function(result) {
		        	fileList = fileList.concat(result.data)
		        	$("#fileList").val(JSON.stringify(fileList));
		        	console.log(JSON.stringify(fileList))
		        }
		    })
		}
		
		
		function downloadFile(path){
			window.open(ctx + path);
		}
		
		function deleteFile(id){
			$(document.getElementById("div"+id)).hide();
			var config = {
        	        url: ctx + "/task/task/deleteFile",
        	        type: 'post',
        	        dataType: 'json',
        	        data: {id:id},
        	        success: function(result) {
        	        	if (typeof callback == "function") {
        	        	    callback(result);
        	        	}
        	        	$.modal.msgSuccess(result.msg);
        	        }
        	    };
        	    $.ajax(config)
		}
		
	</script>
</body>
</html>
